from typing import Callable, Awaitable

from aiogram import BaseMiddleware
from aiogram import types

from database.models import TgUser


class UserGetter(BaseMiddleware):
    """
    Добавляем в **kwargs пользователя
    """
    async def __call__(self, handler: Awaitable | Callable, event: types.Message | types.CallbackQuery, data: dict):
        user: TgUser | None = TgUser.objects.filter(user_id=event.from_user.id).first()
        data['user'] = user
        if user and user.username != event.from_user.username:
            user.username = event.from_user.username
            user.save('Обновление юзернейма.')
        await handler(event, data)
